<?php
	/*
		DreamCMS Version 1.0
		Developed by: Andrei Mikalov (Zeus)
		
		Thanks to Adam Davis
	*/
	include("models/config.php");
	
	//Prevent the user visiting the logged in page if he/she is not logged in
	if(!isUserLoggedIn()) { header("Location: login.php"); die(); }
	
	$pagename= "Update Email"
?>
<?php
	/* 
		Below is a very simple example of how to process a login request.
		Some simple validation (ideally more is needed).
	*/

//Forms posted
if(!empty($_POST))
{
		$errors = array();
		$email = $_POST["email"];

		//Perform some validation
		//Feel free to edit / change as required
		
		if(trim($email) == "")
		{
			$errors[] = lang("ACCOUNT_SPECIFY_EMAIL");
		}
		else if(!isValidEmail($email))
		{
			$errors[] = lang("ACCOUNT_INVALID_EMAIL");
		}
		else if($email == $loggedInUser->email)
		{
				$errors[] = lang("NOTHING_TO_UPDATE");
		}
		else if(emailExists($email))
		{
			$errors[] = lang("ACCOUNT_EMAIL_TAKEN");	
		}
		
		//End data validation
		if(count($errors) == 0)
		{
			$loggedInUser->updateEmail($email);
		}
	}
?>
<?php include("layout_inc/header.php"); ?>
<div id="content_main">
           <h3>Update Email</h3>
            <p>
           <?php
                if(!empty($_POST))
                {
                    if(count($errors) > 0)
                    {
                ?>
                <div id="errors">
                <?php errorBlock($errors); ?>
                </div>     
                <?php
                     } else { ?> 
            <div id="success">
            
               <p><?php echo lang("ACCOUNT_DETAILS_UPDATED"); ?></p>
               
            </div>
            <?php } }?>

    
            <div id="regbox">
                <form name="changePass" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
            
                <p>
                    <label>Email:</label>
                    <input type="text" name="email" value="<?php echo $loggedInUser->email; ?>" />
                </p>
        
                <p>
                    <label>&nbsp;</label>
                    <input type="submit" value="Update Email" class="submit" />
                </p>
                
                </form>
            </p>
                
            
        </div>
    
    <div id="nav_main">
        <h4>Navigation</h4>
        <ul>
          <?php include("layout_inc/right-nav.php"); ?>
        </ul>
    </div>
    
    
</div>

<div id="footer">
<div id="copyright"></div>
Powered by DreamCMS Version 1 <br />
Copyright &copy Andrei Mikalov & Adam Davis <br />
Thanks to Zymic.com for the template.
        <ul id="nav_footer">
        
        </ul>
    
</div>

</body>
</html>
